import ubinascii
from utils.common import hmac_sha256,unix_time,pad_hex,random_uuid4

def generate_clientID(device_id):
    return f"tuyalink_{device_id}"

def generate_username(device_id, timestamp=None):
    if timestamp is None:
        timestamp = unix_time()
    username = (
        f"{device_id}|"
        f"signMethod=hmacSha256,"
        f"timestamp={timestamp},"
        f"secureMode=1,"
        f"accessType=1"
    )
    return username

def generate_password(device_id, device_secret, timestamp=None):
    if timestamp is None:
        timestamp = unix_time()
    
    content = f"deviceId={device_id},timestamp={timestamp},secureMode=1,accessType=1"
    content_bytes = content.encode('utf-8')
    secret_bytes = device_secret.encode('utf-8')

    signature = hmac_sha256(secret_bytes, content_bytes)
    hex_signature = ubinascii.hexlify(signature).decode('utf-8')
    hex_signature = pad_hex(hex_signature)

    print(content)

    return hex_signature

def generate_msg(data):
    return  {
        "msgId": random_uuid4(),
        "time": unix_time(),
        "data": data
    }

if __name__ == "__main__":
    print(generate_username("26de13c127d9c57374zr2x", 1749721257758))
    print(generate_password("26de13c127d9c57374zr2x", "TuyBklArShNrCR2R", 1749721257758))

    print("---------------------------------------------------------------")

    print(generate_username("26de13c127d9c57374zr2x"))
    print(generate_password("26de13c127d9c57374zr2x", "TuyBklArShNrCR2R"))